package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.controller.DataTagPackController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.DataPermQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.DataTagPackService;
import com.denlaku.longan.util.SnowflakeUtils;
import com.denlaku.longan.vo.DataTagPack;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class DataTagPackControllerImpl implements DataTagPackController {
    @Autowired
    private DataTagPackService dataTagPackService;

    @Override
    public Return<Long> add(DataTagPack dataTagPack) {
        dataTagPack.setId(SnowflakeUtils.nextId());
        dataTagPackService.add(dataTagPack);
        return Resp.success(dataTagPack.getId());
    }

    @Override
    public Return<Long> update(DataTagPack dataTagPack) {
        dataTagPackService.update(dataTagPack);
        return Resp.success(dataTagPack.getId());
    }

    @Override
    public Return<DataTagPack> get(Query query) {
        DataTagPack dataTagPack = dataTagPackService.get(query);
        return Resp.success(dataTagPack);
    }

    @VarBy
    @Override
    public Return<List<DataTagPack>> pageList(Query query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<DataTagPack> list = dataTagPackService.list(query);
        return Resp.success(list);
    }

    @Override
    public Return<List<DataTagPack>> search(Query query) {
        List<DataTagPack> list = dataTagPackService.search(query);
        return Resp.success(list);
    }

    @Override
    public Return<Void> delete(DataPermQuery query) {
        if (query.getId() != null) {
            dataTagPackService.delete(query);
        }
        return Resp.success();
    }
}
